package com.yaoyanshe.trialfield.common.base;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.yaoyanshe.trialfield.common.utils.YLogger;


/**
 * <p>description:</p>
 * <p>created on: 2020/5/13 16:06</p>
 *
 * @author tck
 * @version v1.0.0
 */
public abstract class BaseDialogFragment extends DialogFragment {

    public String TAG = getClass().getSimpleName();

    public Context context;
    public OnItemClickListener onItemClickListener;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (getLayoutId() == 0) {
            return getContentView(container);
        } else {
            return inflater.inflate(getLayoutId(), container, false);
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initData();
        setListener(view);
    }

    /**
     * dialog布局
     *
     * @return dialog布局id
     */
    @LayoutRes
    protected int getLayoutId() {
        return 0;
    }

    protected View getContentView(@Nullable ViewGroup container) {
        return null;
    }

    protected void initView(@NonNull View view) {

    }

    protected void initData() {

    }

    protected void setListener(@NonNull View view) {

    }


    /**
     * 关闭软键盘
     */
    protected void toggleKeyboard(View view) {
        if (getActivity() != null) {
            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null && view != null) {
                imm.toggleSoftInputFromWindow(view.getWindowToken(), 0, 0);
            }
        }
    }

    /**
     * 软键盘开关
     */
    protected void closeKeyboard(View view) {
        if (getActivity() != null) {
            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null && view != null) {
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        }
    }

    public BaseDialogFragment setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
        return this;
    }

    /**
     * 设置是否可以取消
     * false 不可取消
     */
    protected void setCanCancel(boolean cancel) {
        if (getDialog() != null) {
            getDialog().setCancelable(cancel);
            getDialog().setCanceledOnTouchOutside(cancel);
            getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        return !cancel;
                    }
                    return false;
                }
            });
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        if (getDialog() != null) {
            Window window = getDialog().getWindow();
            if (window != null) {
                onDialogStart(window);
            }
        }
    }

    protected void onDialogStart(@NonNull Window window) {

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        Dialog dialog = getDialog();
        if (dialog != null) {
            try {
                dialog.setOnDismissListener(null);
                dialog.setOnCancelListener(null);
                dialog.setCancelMessage(null);
                dialog = null;
            } catch (Exception e) {
                YLogger.d("DialogFragment onDestroy error");
            }
        }
    }
}
